package org.jadestudio.home.action;

import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jadestudio.home.service.ArticleService;
import org.jadestudio.home.service.GameService;
import org.jadestudio.home.service.LeaderboardService;

public class SearchAction extends Action {
	private GameService gameService;
	private ArticleService articleService;
	
	public GameService getGameService() {
		return gameService;
	}
	public void setGameService(GameService gameService) {
		this.gameService = gameService;
	}
	
	public ArticleService getArticleService() {
		return articleService;
	}
	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	
	
	/**
	 * 加载数据
	 */
	@SuppressWarnings("unchecked")
	public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		
		String k = request.getParameter("k");
		if (k!=null){
			
			String keyname = URLDecoder.decode(k, "UTF-8");

			// 加载游戏搜索结果
			List<java.util.Map> gameList = gameService.searchGamesByKeyname(keyname);
			request.setAttribute("gameList", gameList);
		
			// 加载文章搜索结果
			List<java.util.Map> articleList = articleService.searchArticlesByKeyname(keyname);
			request.setAttribute("articleList", articleList);
				
			return mapping.findForward("success");
		}
		else{
			response.sendRedirect("/index.do");
			return null;
		}
	}
}
